﻿#include <iostream>
#include <windows.h>
#include <cstring>
#include <stdio.h>
#include <conio.h>

//з ?пцчя??вп? цч?наа з?цчпз?ф? Project -> Properties-> Configuration Properties -> General->Multi-Byte Character Set.
const char lat_key[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const char kir_key[]=" ЎўЈ¤Ґс¦§Ё©Є«¬­®ЇабвгдежзийклмнопЂЃ‚ѓ„…р†‡€‰Љ‹ЊЌЋЏђ‘’“”•–—™љ›Ѓќћџ";
const char pun_key[]=",.:!?()-";
const char num_key[]="1234567890";

struct counters
{
	unsigned counter_lat:2; 
	unsigned counter_kir:2;
	unsigned counter_pun:2;
	unsigned counter_num:2;
	unsigned counter_red:2;
	unsigned counter_newt:2;
} a={0};

enum Key {KeyUp, KeyDown, KeyRight, KeyLeft, KeyEnter, KeyUnknown};
char Lat_Filter(char *,char *);
char Kir_Filter(char *,char *);
char Pun_Filter(char *,char *);
char Num_Filter(char *,char *);
char RedaktingMas(char *);
char Filter_ON(char *,const char *);
char Filter_OFF(char *, char *,const char * );
void Cout_menu(char *);
void Red_Arr(char *);
void New_Arr(char *);
void menu(char *);
void gotoxy ( short , short  );
void EnterMas(char *);
char Redakting(char *);
counters pointer_to_funk(char *,char *,int );
Key readKey();
void main()
{
	 gotoxy(0,0);
	 SetConsoleTextAttribute(GetStdHandle ( STD_OUTPUT_HANDLE ),0x1F);
	 system("cls");
	 const int size=100;
	 char N[size];
	 EnterMas(N);
	 menu(N);
}
void EnterMas(char *arr)
{
	if(a.counter_newt>0)
	{
		SetConsoleTextAttribute(GetStdHandle ( STD_OUTPUT_HANDLE ),0x1F);
			system("cls");
	}
	std::cout<<"Press ECS to EXIT";
	
	int i=0,x=0,y=1;
		while(true)
		{
		if(x<0)
		x=0;
		if(y<0)
		y=0;
		gotoxy(x,y);
		arr[i]=getch();
		if(arr[i]==13)//enter
		{
			arr[i]='\n';
			std::cout<<arr[i];
			x=0;
			y++;
			i++;
			continue;
		}
		if(arr[i]==27)//esc
		{
			arr[i]='\0';
			break;
		}
		if(arr[i]==8)//backspace
		{
			arr[i]='\0';
			i--;
			x--;
			
			gotoxy(x,y);
			std::cout<<" ";
			continue;
		}
		std::cout<<arr[i];
		i++;
		x++;
		}
}
void menu(char *arr)
{	
	
	int num=0;
	char *buf=new char[strlen(arr)];
	const int menuItemsCount = 7;
	char menu[7][128]={{"ўўҐ¤ЁвҐ 1 ¤«п б®авЁа®ўЄЁ « вЁ­бЄЁе ЎгЄў"},{"ўўҐ¤ЁвҐ 2 ¤«п б®авЁа®ўЄЁ ЄЁаЁ«ЁжЁ"},{"ўўҐ¤ЁвҐ 3 ¤«п б®авЁа®ўЄЁ §­ Є®ў Їг­Євг жЁЁ "},{"ўўҐ¤ЁвҐ 4 ¤«п б®авЁа®ўЄЁ жЁда"},{"‚ўҐ¤ЁвҐ 5 ¤«п аҐ¤ ЄвЁа®ў ­Ёп вҐЄбв"},{"‚ўҐ¤ЁвҐ 6 ¤«п ўў®¤  ­®ў®Ј® вҐЄбв"},{"ўўҐ¤ЁвҐ 7 ¤«п ўле®¤ "}};
	int selected = 0;
	short slectedColor = 0x1F;
	short commonColor = 0x30;
	strcpy(buf,arr);
		Cout_menu(buf);
		
		bool showMenu = true;
	do 
	{
		int prevSelected = selected;
		bool selectionChanged = false;
		
		Key key = readKey();

		switch(key)
		{
		case KeyDown:
			if (selected < menuItemsCount - 1)
			{
				selected++;
				selectionChanged = true;
			}
			break;
		case KeyUp:
			if (selected > 0)
			{
				selected--;
				selectionChanged = true;
			}
			break;
		case KeyEnter:
			{
				
					a=pointer_to_funk(arr,buf,selected+1);
					
					gotoxy(0,7);
					SetConsoleTextAttribute(GetStdHandle ( STD_OUTPUT_HANDLE ),slectedColor );
					std::cout<<buf;
					
					if(a.counter_kir>0||a.counter_lat>0||a.counter_pun>0||a.counter_num>0)
					{
						gotoxy(42,selected);
						std::cout<<" +";
					}
					else
					{
						gotoxy(42,selected);
						std::cout<<" -";
					}
					break;
			}
			
		}

		if(selectionChanged)
		{	
			gotoxy(0, prevSelected);
			SetConsoleTextAttribute(GetStdHandle ( STD_OUTPUT_HANDLE ),slectedColor );
			std::cout << menu[prevSelected];

			gotoxy(0, selected);
			SetConsoleTextAttribute(GetStdHandle ( STD_OUTPUT_HANDLE ), commonColor);
			std::cout << menu[selected];
	
		}
		
	} while (showMenu);
		
		
		system("cls");
	
}
counters pointer_to_funk(char *arr,char *buf,int num)
{
	switch (num)
	{	
		case 1:
		{
			Lat_Filter(arr,buf);
			break;
		}
	
		case 2:
		{ 
			Kir_Filter(arr,buf);
			break;
		}

		case 3:
		{ 
			Pun_Filter(arr,buf);
			break;
		}
	
		case 4:
		{ 
			Num_Filter(arr,buf);
				break;
		}
		case 5:
		{
			a.counter_red++;
			if(a.counter_red>1)
			{
				a.counter_red=0;
			}
			Redakting(arr);
			system("cls");
			Cout_menu(buf);
			break;
		}
		case 6:
		{
			a.counter_newt++;
			if(a.counter_newt>1)
			{
				a.counter_newt=0;
			}
			EnterMas(arr);
			system("cls");
			Cout_menu(buf);
			break;
		}
		case 7:
		{
			std::cout<<"\n";
			exit(0);
		}
	}
	
	return a;
}
char Lat_Filter(char *arr,char *buf)
{
		a.counter_lat++;
				if(a.counter_lat==2)
				{
					a.counter_lat=0;
					*buf=Filter_OFF(buf,arr,lat_key);
					return *buf;
				}
		*buf=Filter_ON(buf,lat_key);
		return *buf;
}
char Kir_Filter(char *arr,char *buf)
{
	a.counter_kir++;
				if(a.counter_kir==2)
				{
					a.counter_kir=0;
					*buf=Filter_OFF(buf,arr,kir_key);
					return *buf;
				}
				*buf=Filter_ON(buf,kir_key);
				return *buf;
}
char Pun_Filter(char *arr,char *buf)
{
a.counter_pun++;
			if(a.counter_pun==2)
			{
				a.counter_pun=0;
				*buf=Filter_OFF(buf,arr,pun_key);
				return *buf;
			}
				*buf=Filter_ON(buf,pun_key);
				return *buf;
}
char Num_Filter(char *arr,char *buf)
{
a.counter_num++;
			if(a.counter_num==2)
			{
				a.counter_num=0;
				*buf=Filter_OFF(buf,arr,num_key);
				return *buf;
			}
				*buf=Filter_ON(buf,num_key);
return *buf;
}
char Redakting(char *arr)
{
	
	std::cout<<"Press ECS to EXIT";
	short slectedColor =0x1F;
	short commonColor  =0x30;
	gotoxy(0,0);
	SetConsoleTextAttribute(GetStdHandle ( STD_OUTPUT_HANDLE ),slectedColor);
	system("cls");
	std::cout<<arr;
	int lenArr=strlen(arr);
	int selected=0;
	unsigned int y=0,x=0;
	do
	{
		if (x<0){x=0;}
		if (y<0){y=0;}
		int lenArr=strlen(arr);
		int prevSelected = selected;
		bool selectionChanged=false;
		gotoxy(x, y);
		char key=getch();
		switch(key)
		{
			case 77://right
				if (selected < lenArr)
				{
					selected++;x++;
					selectionChanged = true;
					if(arr[selected]==13&&y<1)
					{
						x=selected;
						y++;
					}
				}
				break;
			case -32:
					break;
			case 75://left
				if (selected > 0)
				{
					selected--;x--;
					selectionChanged = true;
					if(arr[selected]==13&&y<1)
					{
						x=selected;
						y--;
					}
				}
				break;
			case 13://enter
				{
					arr[selected]='/n';
					x=0;
					y++;
					break;
				}
			case 8://Backspace
				{
					system("cls");
					char *pWithBackspace=&arr[selected];
					strcpy(pWithBackspace,&arr[selected+1]);
					std::cout<<arr;
					break;
				}
			case 27://ESC
				{
					return *arr;
				}
			default:
				{
				char *buf_mas=new char[strlen(arr)+1];
				strncpy(buf_mas,arr,selected+1);
				buf_mas[selected+1]=key;
				strcpy(&buf_mas[selected+2],&arr[selected]);
				strcpy(arr,buf_mas);
				}
			}
		
	
		if(arr[selected]==13&&y<1)
		{
			y++;
		}
		if(selectionChanged)
		{	
			gotoxy(x, y);
			std::cout << &arr[selected];
			
		}
	}
		while(true);	


}
char Filter_ON(char *arr,const char *key)
{	
	
	char *pmas;
	pmas = strpbrk (arr, key);                        
		while (pmas != NULL)                                         
		{
			pmas[0]='_';                            
			pmas = strpbrk (pmas+1,key); 
		}
		
	return *arr;
			

}
char Filter_OFF(char *arr, char *enter_arr,const char *key)
{
	char *pmas;
	pmas = strpbrk (enter_arr, key); 
	if(pmas==NULL)
	{
		return *arr;
	}
	int len=strlen(enter_arr)-(strlen(pmas));
	while (pmas != NULL)                                         
		{
			arr[len]=pmas[0];                         
			pmas = strpbrk (pmas+1,key); 
				if(pmas==NULL)
					{
						return *arr;
					}
				len=strlen(enter_arr)-(strlen(pmas));
		
		}
	return *arr;
}
Key readKey()
{
	unsigned char a = getch();
	if (a == 224)
	{
		unsigned char b = getch();
		switch(b)
		{
		case 72:
			return KeyUp;
		case 80:
			return KeyDown;
		case 75:
			return KeyLeft;
		case 77:
			return KeyRight;
		}		
	}
	else if (a == 13)
		return KeyEnter;
	
	return KeyUnknown;
}	
void gotoxy ( short x, short y )
{
	COORD coord = {x, y};
	SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), coord );
}
void Cout_menu(char *arr )
{
	short slectedColor = 0x1F;
	char menu[7][128]={{"ўўҐ¤ЁвҐ 1 ¤«п б®авЁа®ўЄЁ « вЁ­бЄЁе ЎгЄў"},{"ўўҐ¤ЁвҐ 2 ¤«п б®авЁа®ўЄЁ ЄЁаЁ«ЁжЁ"},{"ўўҐ¤ЁвҐ 3 ¤«п б®авЁа®ўЄЁ §­ Є®ў Їг­Євг жЁЁ "},{"ўўҐ¤ЁвҐ 4 ¤«п б®авЁа®ўЄЁ жЁда"},{"‚ўҐ¤ЁвҐ 5 ¤«п аҐ¤ ЄвЁа®ў ­Ёп вҐЄбв"},{"‚ўҐ¤ЁвҐ 6 ¤«п ўў®¤  ­®ў®Ј® вҐЄбв"},{"ўўҐ¤ЁвҐ 7 ¤«п ўле®¤ "}};

	gotoxy(0,0);
		SetConsoleTextAttribute(GetStdHandle ( STD_OUTPUT_HANDLE ), slectedColor);
		system("cls");
		int i;
		for(i=0;i<7;i++)
		{
		gotoxy(0,i);
		std::cout<<menu[i];
		}
		gotoxy(0,i);
		std::cout<<arr<<"\n";
}
